<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="编号" prop="id">
        <el-input
          v-model="queryParams.id"
          placeholder="请输入编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="昵称" prop="catName">
        <el-input
          v-model="queryParams.catName"
          placeholder="请输入昵称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="种类" prop="catType">
        <el-select v-model="queryParams.catType" placeholder="请选择种类" clearable>
          <el-option
            v-for="dict in dict.type.cat_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="性别" prop="catGender">
        <el-select v-model="queryParams.catGender" placeholder="请选择性别" clearable>
          <el-option
            v-for="dict in dict.type.cat_gender"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>



      <el-form-item label="编辑人" prop="catAuthor">
        <el-input
          v-model="queryParams.catAuthor"
          placeholder="请输入编辑人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="更新时间" prop="catUpdate">
        <el-date-picker clearable
          v-model="queryParams.catUpdate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择更新时间">
        </el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['wx:wxcatMessage:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['wx:wxcatMessage:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['wx:wxcatMessage:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['wx:wxcatMessage:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="wxcatMessageList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="猫咪美照" align="center" prop="catImg" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.catImg" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="昵称" align="center" prop="catName" />
      <el-table-column label="种类" align="center" prop="catType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.cat_type" :value="scope.row.catType"/>
        </template>
      </el-table-column>
      <el-table-column label="性别" align="center" prop="catGender">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.cat_gender" :value="scope.row.catGender"/>
        </template>
      </el-table-column>
      <el-table-column label="首次发现" align="center" prop="catView" >
        <template slot-scope="scope">
          <span style="display: -webkit-box;
          -webkit-line-clamp: 2;
          -webkit-box-orient: vertical;
          overflow: hidden;">{{scope.row.catView}}</span>
        </template>
      </el-table-column>
      <el-table-column label="情况" align="center" prop="catSituation">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.cat_state" :value="scope.row.catSituation"/>
        </template>
      </el-table-column>
      <el-table-column label="性格" align="center" prop="catCharacter">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.cat_characteristic" :value="scope.row.catCharacter"/>
        </template>
      </el-table-column>
      <el-table-column label="绝育情况" align="center" prop="catBirth">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.cat_birth" :value="scope.row.catBirth"/>
        </template>
      </el-table-column>
      <el-table-column label="送养时间" align="center" prop="catAdoption" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.catAdoption, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="离世时间" align="center" prop="catDie" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.catDie, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="外貌" align="center" prop="catMoreAppearance" >
        <template slot-scope="scope">
          <span style="display: -webkit-box;
          -webkit-line-clamp: 2;
          -webkit-box-orient: vertical;
          overflow: hidden;">{{scope.row.catMoreAppearance}}</span>
        </template>
      </el-table-column>
      <el-table-column label="出没地点" align="center" prop="catMorePlace">
        <template slot-scope="scope">
          <span style="display: -webkit-box;
          -webkit-line-clamp: 2;
          -webkit-box-orient: vertical;
          overflow: hidden;">{{scope.row.catMorePlace}}</span>
        </template>
      </el-table-column>
      <el-table-column label="关系" align="center" prop="catMoreRelationship" >
        <template slot-scope="scope">
          <span style="display: -webkit-box;
          -webkit-line-clamp: 2;
          -webkit-box-orient: vertical;
          overflow: hidden;">{{scope.row.catMoreRelationship}}</span>
        </template>
      </el-table-column>

      <el-table-column label="更多" align="center" prop="catMoreDetail">
        <template slot-scope="scope">
          <span style="display: -webkit-box;
          -webkit-line-clamp: 2;
          -webkit-box-orient: vertical;
          overflow: hidden;">{{scope.row.catMoreDetail}}</span>
        </template>
      </el-table-column>

      <el-table-column label="编辑人" align="center" prop="catAuthor" />
      <el-table-column label="更新时间" align="center" prop="catUpdate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.catUpdate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['wx:wxcatMessage:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['wx:wxcatMessage:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改猫咪图鉴对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="猫咪美照">
          <image-upload v-model="form.catImg"/>
        </el-form-item>
        <el-form-item label="昵称" prop="catName">
          <el-input v-model="form.catName" placeholder="请输入昵称" />
        </el-form-item>
        <el-form-item label="种类" prop="catType">
          <el-select v-model="form.catType" placeholder="请选择种类">
            <el-option
              v-for="dict in dict.type.cat_type"
              :key="dict.value"
              :label="dict.label"
:value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="性别" prop="catGender">
          <el-select v-model="form.catGender" placeholder="请选择性别">
            <el-option
              v-for="dict in dict.type.cat_gender"
              :key="dict.value"
              :label="dict.label"
:value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="首次发现" prop="catView">
          <el-input v-model="form.catView" placeholder="请输入首次发现" />
        </el-form-item>
        <el-form-item label="情况" prop="catSituation">
          <el-select v-model="form.catSituation" placeholder="请选择情况">
            <el-option
              v-for="dict in dict.type.cat_state"
              :key="dict.value"
              :label="dict.label"
:value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="性格" prop="catCharacter">
          <el-select v-model="form.catCharacter" placeholder="请选择性格">
            <el-option
              v-for="dict in dict.type.cat_characteristic"
              :key="dict.value"
              :label="dict.label"
:value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="绝育情况" prop="catBirth">
          <el-select v-model="form.catBirth" placeholder="请选择绝育情况">
            <el-option
              v-for="dict in dict.type.cat_birth"
              :key="dict.value"
              :label="dict.label"
:value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="送养时间" prop="catAdoption">
          <el-date-picker clearable
            v-model="form.catAdoption"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择送养时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="离世时间" prop="catDie">
          <el-date-picker clearable
            v-model="form.catDie"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择离世时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="外貌" prop="catMoreAppearance">
          <el-input v-model="form.catMoreAppearance" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="出没地点" prop="catMorePlace">
          <el-input v-model="form.catMorePlace" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="关系" prop="catMoreRelationship">
          <el-input v-model="form.catMoreRelationship" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="更多" prop="catMoreDetail">
          <el-input v-model="form.catMoreDetail" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="编辑人" prop="catAuthor">
          <el-input v-model="form.catAuthor" placeholder="请输入编辑人" />
        </el-form-item>
        <el-form-item label="更新时间" prop="catUpdate">
          <el-date-picker clearable
            v-model="form.catUpdate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择更新时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listWxcatMessage, getWxcatMessage, delWxcatMessage, addWxcatMessage, updateWxcatMessage } from "@/api/wx/wxcatMessage";

export default {
  name: "WxcatMessage",
  dicts: ['cat_state', 'cat_birth', 'cat_gender', 'cat_characteristic', 'cat_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 猫咪图鉴表格数据
      wxcatMessageList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        id: null,
        catImg: null,
        catName: null,
        catType: null,
        catGender: null,
        catView: null,
        catSituation: null,
        catCharacter: null,
        catBirth: null,
        catAdoption: null,
        catDie: null,
        catMoreAppearance: null,
        catMorePlace: null,
        catMoreRelationship: null,
        catMoreDetail: null,
        catAuthor: null,
        catUpdate: null,
        remark: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        catImg: [
          { required: true, message: "猫咪美照不能为空", trigger: "blur" }
        ],
        catName: [
          { required: true, message: "昵称不能为空", trigger: "blur" }
        ],
        catType: [
          { required: true, message: "种类不能为空", trigger: "change" }
        ],
        catGender: [
          { required: true, message: "性别不能为空", trigger: "change" }
        ],
        catSituation: [
          { required: true, message: "情况不能为空", trigger: "change" }
        ],
        catCharacter: [
          { required: true, message: "性格不能为空", trigger: "change" }
        ],
        catBirth: [
          { required: true, message: "绝育情况不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询猫咪图鉴列表 */
    getList() {
      this.loading = true;
      listWxcatMessage(this.queryParams).then(response => {
        this.wxcatMessageList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        catImg: null,
        catName: null,
        catType: null,
        catGender: null,
        catView: null,
        catSituation: null,
        catCharacter: null,
        catBirth: null,
        catAdoption: null,
        catDie: null,
        catMoreAppearance: null,
        catMorePlace: null,
        catMoreRelationship: null,
        catMoreDetail: null,
        catAuthor: null,
        catUpdate: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加猫咪图鉴";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getWxcatMessage(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改猫咪图鉴";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateWxcatMessage(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWxcatMessage(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除猫咪图鉴编号为"' + ids + '"的数据项？').then(function() {
        return delWxcatMessage(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('wx/wxcatMessage/export', {
        ...this.queryParams
      }, `wxcatMessage_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
